package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.lcb.manager.configuration.Config;
import com.lcb.manager.entity.Coal;
import com.lcb.manager.entity.CoalOutput;
import com.lcb.manager.repository.CoalOutputRepository;
import com.lcb.manager.repository.CoalRepository;
import com.lcb.manager.vo.CoalVo;
import com.lcb.manager.vo.Json;
import com.lcb.manager.vo.YearOutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/coal")
public class CoalController {

    @Autowired
    private CoalRepository coalRepository;

    @Autowired
    private CoalOutputRepository coalOutputRepository;

    @RequestMapping(value = "/")
    public String index() {
        return "coal";
    }

    @RequestMapping(value = "/add_template")
    public String addTemplate(ModelMap model) {
        List<Coal> coalList = coalRepository.findAll();
        model.put("coalList", coalList);
        return "_add_coal_output";
    }

    @ResponseBody
    @RequestMapping(value = "/get_name")
    public String getName() {
        Json result = new Json();
        List<Coal> coalList = coalRepository.findAll();
        CoalVo vo = new CoalVo();
        vo.setName1(coalList.get(0).getName());
        vo.setName2(coalList.get(1).getName());
        vo.setName3(coalList.get(2).getName());
        vo.setName4(coalList.get(3).getName());
        vo.setName5(coalList.get(4).getName());
        List<CoalVo> voList = new ArrayList<>();
        voList.add(vo);
        result.code = 0;
        result.data = voList;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit_name")
    public String editName(@RequestParam String field, @RequestParam String name) {
        int id = 1;
        switch (field) {
            case "name1":
                id = 1;
                break;
            case "name2":
                id = 2;
                break;
            case "name3":
                id = 3;
                break;
            case "name4":
                id = 4;
                break;
            case "name5":
                id = 5;
            default:
                break;
        }
        Coal coal = coalRepository.findById(id).orElse(null);
        if (coal != null) {
            coal.setName(name);
            coalRepository.save(coal);
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/get_output")
    public String get_output(@RequestParam(defaultValue = "") String date, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<CoalOutput> pageResult;
        if (!date.isEmpty()) {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            pageResult = coalOutputRepository.findByDateOrderByDateAsc(year, month, pageable);
        } else {
            pageResult = coalOutputRepository.findByOrderByDateAsc(pageable);
        }
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public String add(@RequestParam Date date, @RequestParam String data) {
        Map<Integer, Integer> dataMap = JSON.parseObject(
                data, new TypeReference<Map<Integer, Integer>>() {
                });
        CoalOutput coalOutput = new CoalOutput();
        coalOutput.setDate(date);
        coalOutput.setOutput1(dataMap.get(1));
        coalOutput.setOutput2(dataMap.get(2));
        coalOutput.setOutput3(dataMap.get(3));
        coalOutput.setOutput4(dataMap.get(4));
        coalOutput.setOutput5(dataMap.get(5));
        coalOutputRepository.save(coalOutput);
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/edit_output")
    public String editOutput(CoalOutput coalOutput) {
        coalOutputRepository.save(coalOutput);
        return "ok";
    }

}
